home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / exams.arc / EXAMS.C
Encoding:
C/C++ Source or Header  |  1985-09-20  |  7.3 KB  |  265 lines

  1. /* This    file contains several examples that I have written.  They may be
  2.  * Used    freely for personal non-profit purposes.  All have been    tested
  3.  * on a    VERSION    7 compatible machine, and appear to work O.K.
  4.  * Some    of the examples    can be turned into functional programs easily.
  5.  * Gerald Shrum.  (316)    584-2501    30 Aug., 1985.
  6.  */
  7.  
  8. /* The 'fork' system call starts a new proccess.
  9.    This    is an example program which starts a child and monitors    the progress
  10.    of both the child and the parent
  11.    To see what happens when parent completes before child, make    child_loop
  12.    larger then parent_loop    */
  13. /* Gerry Shrum    22 Feb,    1985    */
  14.  
  15.  
  16. /*---->> Includes and LIB definitions:      preprocessor             <<----<< */
  17. #include    <stdio.h>
  18. #include    <signal.h>
  19. #include    <calls.h>
  20.  
  21. /*---->> Globals and declarations                     <<----<< */
  22. int pid;  /* Child process ID */
  23. int status; /* Child status */
  24.  
  25. /*---->> Functions and modules:                         <<----<< */
  26. /* None    except the predefined system functions:
  27.     exit();
  28.     fork();
  29.     kill();
  30.     printf();
  31.     onalarm();
  32.     sleep();
  33.   See system manual pages for these functions and system calls.
  34.  */
  35.  
  36. /*---->> Program main:                             <<----<< */
  37. main()
  38. {
  39.   int  i, p, sec = 5;
  40.   int  child_loop = 5;    /* Number of times the child loops <<--    User setable */
  41.   int  parent_loop = 10;/* Number of times the parent loops <<-    User setable */
  42.  
  43.     printf("Start of fork example\n");
  44.     pid = fork();  /* call fork to start the child process */
  45.                /* A confusing point is that pid    is returned to both
  46.               parent and child.  The child's real pid is returned
  47.               to the parent, while 0 is returned to    the child iff
  48.               it was started, else the child receives -1         */
  49.  
  50.     if (pid    == 0)  /* iff child was    started    */
  51.         {
  52.          printf("Child started\n");
  53.          status    = 1;/* Let parent know */
  54.          for (i=0; i< child_loop; ++i)/* Now, loop until we reach the
  55.                           number of    loops in 'child_loop' */
  56.             { /* Child loop    body */
  57.              printf("Child still here\n");
  58.              sleep(sec);
  59.             }
  60.  
  61.          printf("Child completed!\n");/* child completed before    parent */
  62.          status    = 0;
  63.          exit(0);/* Child goes zomb until parent completes,        */
  64.         }     /* then system    removes    them both from the pid list */
  65.  
  66. /* Parent comes    here           */
  67.     printf("Parent arrived\n");
  68.  
  69.     if(pid <= 0)
  70.         {
  71.          printf("Couldn't start    child..    Aborting!\n");
  72.          exit(0);
  73.         }
  74.  
  75.     for (p = 0; p <    parent_loop; ++p) /* loop and monitor status */
  76.         {
  77.          printf("Parent: Child PID = %d    \n",pid);
  78.          sleep(sec);
  79.         }
  80.  
  81.     if(status == 1)    /* kill    the child if it    is still running */
  82.         {
  83.          printf("Parent    completed, killing child %d",pid);
  84.          kill(pid,SIGKILL);
  85.         }
  86.  
  87.     printf("Parent completed.\n");
  88.     exit(0);
  89. }
  90.  
  91. /* Program timeout;  Source timeout.c     */
  92. /* timeout:  set time limit on a process */
  93. /* This    program    includes an example of the 'fork' system call.
  94.    It also includes the    pre-defined system function 'onalarm()'.
  95.  
  96.  
  97. #include    <stdio.h>
  98. #include    <signal.h>
  99. #include    <calls.h>
  100. int pid;    /* child process id */
  101. char *progname;
  102.  
  103. main(argc, argv)
  104.     int argc;
  105.     char *argv[];
  106. {
  107.     int sec    = 10, status, onalarm();
  108.  
  109.     progname = argv[0];
  110.     if (argc > 1 &&    argv[1][0] == '-') {/* Then we have 'time' */
  111.         sec = atoi(&argv[1][1]);
  112.         argc--;
  113.         argv++;
  114.         }
  115.     if (argc < 2)/*    Then we    do not have a program to time */
  116.         {
  117.         printf("\n :Usage: timeout [-<t>] [command]\n");
  118.         printf("Where <t> = timeout count in seconds from 1 to 99\n")
  119.         printf("And [command] is the command to    be ran.\n");
  120.         printf("example: timeout -10 cat afile\n");
  121.         exit(1);
  122.         }
  123.     if ((pid=fork()) == 0) {
  124.         execv(argv[1], &argv[1]);
  125.         /* only    returns    on error */
  126.         fprintf(stderr,"\nCouldn't start %s.\n",argv[1]);
  127.         exit(errno);
  128.         }
  129.     signal(SIGALRM,onalarm);
  130.     alarm(sec);
  131.     if (wait(&status) == -1    || (status & 0177) !=0)
  132.         fprintf(stderr,"Timeout: %s killed\n", argv[1]);
  133.         exit(errno);
  134. }
  135.  
  136.     onalarm()    /* kill    child when alarm arrives */
  137.     {
  138.         kill(pid, SIGKILL);
  139.     }
  140.  
  141. /* End of timeout.c */
  142.  
  143.  
  144. /* example of index.    Gerry Shrum    Nov, 1984                */
  145. /* index returns a memory pointer.  this routine converts it to    string pos. */
  146.  
  147.        char tstr[] = {"abcdefghijklmnopqrstuvwxyz1234567890-=,./;':<>?~"};
  148.  
  149. main()
  150. {
  151.     char cch, inp[81];
  152.     int res,fnd, ptr;
  153.  
  154.     fnd = &tstr;
  155.     while(cch != 'Q')
  156.     {
  157.     printf("Enter character    to be found (Q to quit).");
  158.     scanf ("%c",inp);
  159.     cch = inp[0];
  160.     ptr = index(tstr,cch);
  161.     res = ptr - fnd;
  162.     if(ptr == 0) {printf("Not found\n");}
  163.     else printf("%c    = position %d\n",cch,res);
  164.     }
  165. }/* End    of index.c */
  166.  
  167.  
  168.  
  169. /* Pipe1; pipe example.     This file contains the    parent.     The child
  170.    is contained    in pipe2_exam.c.  Compile seperately and link together.
  171.    E.G.:  icc pipe1_exam; icc pipe2_exam
  172.       ilink    pipe1_exam pipe2_exam
  173.    Gerry Shrum,     Nov., 1984.
  174.  */
  175. /*---->> Includes and LIB definitions:      preprocessor               <<----                   << */
  176. #include    <stdio.h>
  177. #include    <sgtty.h>
  178. #include    <signal.h>
  179.  
  180.        int  pid;        /* Child process ID */
  181.        int  status;        /* Child status        */
  182.        int  fildes[2];        /* The pipe file descriptor */
  183.        char out_buf[MAXLINE];    /* buffer for output */
  184.        char in_buf[MAXLINE];    /* buffer for input  */
  185.        char *out_ptr;        /* Pointer to char to be output    */
  186.  
  187. /* ***** ******* ******* ******* ******* ******* ******* ******* ******* **** */
  188. main()
  189.  
  190.     {
  191.     int i, in_stat,    child_proc, yy;
  192.  
  193.      in_stat = pipe(fildes);/* 0 returned iff pipe was opened, else    -1 */
  194.      if(in_stat == -1)/* too many files open */
  195.         {
  196.          printf("Can't open pipe!\n");
  197.          exit(in_stat);
  198.         }
  199.      printf("pipe_rd =%d, pipe_wrt =%d, rtn    =%d\n",fildes[0],fildes[1],in_stat);
  200.      pid = fork();
  201.      if (pid == 0) /* Iff child was    started    */
  202.         {
  203.          write_pipe();
  204.          printf("Pipe broke;  Aborting...\n");
  205.          status    = 0;
  206.          exit(0);   /* Iff child ever returns */
  207.         }
  208.     /* Parent comes    here */
  209.      if(pid    <= 0)
  210.         {
  211.          printf("Couldn't start    child; Aborting...");
  212.          exit(14);
  213.         }
  214.      printf("Child started as %d, status =%d\n",pid,status);
  215.      close(fildes[1]); /* this function doesn't write */
  216.      for(i=0;i<10;++i)
  217.         {
  218.          sleep(1);
  219.          yy = read(fildes[0],out_buf,20);/* Read from child */
  220.          printf("Read %d characters.%s\n",yy,out_buf);
  221.          if (yy    == -1 )    {printf("Read pipe status =%d\n",yy);}
  222.         }
  223.  
  224.      close(fildes[0]);    /* close the read pipe */
  225.      kill(pid,SIGKILL);    /* Terminate the child */
  226.     exit(0)    ;
  227.     } /* End of pipe1.c */
  228.  
  229.  
  230.  
  231.  
  232. /* Pipe    example    #2, child process writing to parent.        */
  233. /* Gerry Shrum,    Jan 1985                    */
  234. /* write from this child process to the    calling    process        */
  235. /* fildes[0] is    the read descriptor from the parent pipe call    */
  236. /* fildes[1] is    the write descriptor from the parent pipe call    */
  237.  
  238. /*---->> Includes and LIB definitions:      preprocessor             <<----<< */
  239. #include    <stdio.h>
  240.  
  241. /*---->> Globals and declarations                     <<----<< */
  242. extern    int fildes[2];
  243.  
  244. /*---->> Functions and modules:                         <<----<< */
  245. VOID   write_pipe()
  246.        {
  247.     char bufin[24];
  248.     int  i,    fd, rstat;
  249.  
  250.     *bufin = "Writing into a pipe'\n'";
  251.     fd = fildes[1];
  252.     close(fildes[0]);/* This function doesn't read */
  253.     for(i=0;i<10;++i)
  254.         {
  255.          /* write on the pipe */
  256.          rstat = write(fd,bufin,20);
  257.          if(rstat == -1) {printf("pipe write error  %d\n",rstat);}
  258.         }
  259.     close(fildes[1]);
  260.     exit(0)    ;
  261.     } /* End of pipe2.c */
  262.  
  263. /* End of file */
  264. /* End of exams.c */
  265. ro